using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ProductInterfaces;
using AccesoDatos;

namespace ProductBE
{
    public class Product : IProduct
    {
        private int __catID = 0;

        private Manager __objMgr = null;

        internal Product(int lpCatID)
        {
            this.__catID = lpCatID;
            this.__objMgr = new Manager("conexion.xml"); 
        }

        private ParametroCollection ParamSelect()
        {
            ParametroCollection lvPi = new ParametroCollection();
            lvPi.Add(new Parametro("@cat_id",  this.__catID, Parametro.TipoDir.Entrada));      
            return lvPi;
        }

        private DataTable __dtbSource = null;

        public DataTable DataSource
        {
            get { return __dtbSource; }            
        }

        public DataTable Read()
        {
            DataTable lvDtbRet = null;
            try
            {
                this.__objMgr.Conectar();

                this.__dtbSource = lvDtbRet = this.__objMgr.Leer("dbo.usps_Products", 
                             this.ParamSelect()).Tables[0];
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to Read products.", ex);
            }
            finally
            {
                this.__objMgr.Desconectar();
            }
            return lvDtbRet; 
        }

        public void Save()
        {
            this.__dtbSource.GetChanges(DataRowState.Added);      
        }
    }
}
